import Vue from 'vue'
import { login,getPermission} from "@/api/index"
import store from '@/store'
//用户信息的仓库
//, logout,queryPermissionsByUser

const user = {
  state: {
    token: '',
    username: '',
    avatar: '',
    permissionList: [],
    info: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, username) => {
      state.username = username
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_PERMISSIONLIST: (state, permissionList) => {
      state.permissionList = permissionList
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          if(response.code =='200'){
            const result = response.data
            Vue.local.set("ACCESS_TOKEN", result.token, 7 * 24 * 60 * 60 * 1000)
            Vue.local.set("USER_NAME", result.name, 7 * 24 * 60 * 60 * 1000)
            Vue.local.set("USER_INFO",result.info, 7 * 24 * 60 * 60 * 1000)
            Vue.local.set("USER_AVATAR",result.info.avatarUrl, 7 * 24 * 60 * 60 * 1000)
            console.log(Vue.local.get("USER_AVATAR"))
            commit('SET_TOKEN', result.token)
            commit('SET_INFO', result.info)
            commit('SET_NAME', result.name)
            commit('SET_AVATAR', result.info.avatarUrl)
            resolve(response)
          }else{
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetPermissionList({ commit }) {
      
      return new Promise((resolve, reject) => {
        getPermission().then((res)=>{
            
            if(res.data.permissionList.length!==0){
              console.log(res)
            let menuData
            if(res.code===200){
              
              menuData=res.data.permissionList
              commit('SET_PERMISSIONLIST', menuData)
              console.log(res.data.permissionLis)
              resolve(menuData)
            }else{
              reject("登录过期")
            }
            resolve(menuData)
              
            }else{
              Vue.prototype.$Modal.error({
                title:'登录已过期',
                content:'很抱歉，登录已过期，请重新登录',
                okText:'重新登陆',
                mask:true,
                onOk:()=>{
                    store.dispatch('Logout').then(()=>{
                        Vue.local.remove("ACCESS_TOKEN")
                        try{
                            let path=window.document.location.pathname
                            if(path!='/'&& path.indexOf('/user/login')==-1){
                                window.location.reload()
                            }
                        }catch(e){
                            window.location.reload()
                        }
                    })
                }
            })
            }
        })
      })
    },

    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        let logoutToken = state.token;
        commit('SET_TOKEN', '')
        commit('SET_PERMISSIONLIST', [])
        Vue.local.remove("ACCESS_TOKEN")
        Vue.local.remove("USER_INFO")
        Vue.local.remove("USER_NAME")
        //console.log('logoutToken: '+ logoutToken)
        // logout(logoutToken).then(() => {
          
        resolve()
        // }).catch(() => {
        //   resolve()
        // })
      })
    },


  }
}

export default user